﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using HouseSecretary.Model;
using DevExpress.XtraEditors.Controls;
using System.IO;
using System.Drawing.Text;
using Newtonsoft.Json;
using System.Text.RegularExpressions;

namespace HouseSecretary.HouseForm.HouseSrcInfoWebDCLookCtrl
{
    public partial class HouseSrcInfoWebDCLook_BasicControl : UserControl
    {
        bool isIMGshow = false;
        int intLookCnt = 0;

        /// <summary>
        /// 参数模型
        /// </summary>
        private Model.CommonDlgParameters CommDlgParaModel;
        /// <summary>
        /// 房源
        /// </summary>
        Model.HouseSrcInfoWebDC houseSrcInfoWebDCModel = new Model.HouseSrcInfoWebDC();
        
        public HouseSrcInfoWebDCLook_BasicControl(Model.CommonDlgParameters CommonDlgParameters)
        {
            this.CommDlgParaModel = CommonDlgParameters;
            InitializeComponent();
        } 

        private void HouseSrcInfoWebDCLook_BasicControl_Load(object sender, EventArgs e)
        {
            houseSrcInfoWebDCModel = CommDlgParaModel.QueryModelList[0] as Model.HouseSrcInfoWebDC; //获取房源

            InitComponent();

            #region 设置 只读

            this.txtFHouseSrcInfoID.Properties.ReadOnly = true;
            this.txtFFloor.Properties.ReadOnly = true; ;
            this.txtFTotalFloor.Properties.ReadOnly = true;
            this.txtFArea.Properties.ReadOnly = true;
            this.txtFPrice.Properties.ReadOnly = true;
            this.txtDoorModel.Properties.ReadOnly = true;
            //行政区
            this.txtFDistrictNo4.Properties.ReadOnly = true;
            this.txtFAddr.Properties.ReadOnly = true;
            this.txtFDCSource.Properties.ReadOnly = true;
            this.dteFDCDate.Properties.ReadOnly = true;
            this.dteFDCDate.Properties.AllowDropDownWhenReadOnly = DevExpress.Utils.DefaultBoolean.False;
            this.txtFTitle.Properties.ReadOnly = true;
            this.txtFHouseOwner.Properties.ReadOnly = true;
            //房东电话
            this.txtFHouseOwnerTelText.Properties.ReadOnly = true;
            this.picFHouseOwnerTelPic.Properties.ReadOnly = true;
            this.dteFWebRegDate.Properties.ReadOnly = true;
            this.dteFWebRegDate.Properties.AllowDropDownWhenReadOnly = DevExpress.Utils.DefaultBoolean.False;
            this.txtFNote.Properties.ReadOnly = true;

            #endregion

            this.txtFHouseSrcInfoID.Text = houseSrcInfoWebDCModel.FHouseSrcInfoID.ToString();
            if (houseSrcInfoWebDCModel.FRentSale == "C" || houseSrcInfoWebDCModel.FRentSale == "R")
            {
                this.picFRentSale.EditValue = Properties.Resources.icon_rent;
            }
            else if (houseSrcInfoWebDCModel.FRentSale == "S")
            {
                this.picFRentSale.EditValue = Properties.Resources.icon_sail;
            }
            this.txtFFloor.Text = houseSrcInfoWebDCModel.FFloor == null ? "" : houseSrcInfoWebDCModel.FFloor.ToString();
            this.txtFTotalFloor.Text = houseSrcInfoWebDCModel.FTotalFloor == null ? "" : houseSrcInfoWebDCModel.FTotalFloor.ToString();
            this.txtFArea.Text = houseSrcInfoWebDCModel.FArea == null ? "" : houseSrcInfoWebDCModel.FArea.ToString();
            //如果是售，则单位为万元；如果是租，则为元/月
            if (houseSrcInfoWebDCModel.FRentSale != "S")
            {
                lblFPriceUnit.Text = "元/月";
                this.txtFPrice.Text = RentPrice(houseSrcInfoWebDCModel.FPrice);
            }
            else
            {
                this.txtFPrice.Text = SalePrice(houseSrcInfoWebDCModel.FPrice);
            }
            this.txtDoorModel.Text = houseSrcInfoWebDCModel.DoorModel;
            //行政区
            this.txtFDistrictNo4.Text = houseSrcInfoWebDCModel.FDistrictNo4;
            this.txtFAddr.Text = houseSrcInfoWebDCModel.FAddr;
            this.txtFDCSource.Text = houseSrcInfoWebDCModel.FDCSource;
            this.dteFDCDate.EditValue = houseSrcInfoWebDCModel.FDCDate;
            this.txtFTitle.Text = houseSrcInfoWebDCModel.FTitle;
            this.txtFHouseOwner.Text = houseSrcInfoWebDCModel.FHouseOwner;
            //房东电话
            if (houseSrcInfoWebDCModel.FIsTelText == "Y")
            {
                this.txtFHouseOwnerTelText.Visible = true;
                this.picFHouseOwnerTelPic.Visible = false;

                this.txtFHouseOwnerTelText.Text = houseSrcInfoWebDCModel.FHouseOwnerTelText;
            }
            else
            {
                this.txtFHouseOwnerTelText.Visible = false;
                this.picFHouseOwnerTelPic.Visible = true;

                this.picFHouseOwnerTelPic.EditValue = houseSrcInfoWebDCModel.FHouseOwnerTelPic;
            }

            intLookCnt = houseSrcInfoWebDCModel.FLookCnt + 1;
            if (houseSrcInfoWebDCModel.FStatus == "2")//已经入库
            {
                isIMGshow = true;
            }
            else
            {
                isIMGshow = false;
            }
            this.dteFWebRegDate.EditValue = houseSrcInfoWebDCModel.FWebRegDate;
            this.txtFNote.Text = houseSrcInfoWebDCModel.FNote;



            //查看次数增加1（需要API程序）

        }

        /// <summary>
        /// 初始化控件
        /// </summary>
        private void InitComponent()
        {

            //房东电话
            this.picFHouseOwnerTelPic.Left = this.txtFHouseOwnerTelText.Left;
            this.picFHouseOwnerTelPic.Top = this.txtFHouseOwnerTelText.Top;
            this.picFHouseOwnerTelPic.Width = this.txtFHouseOwnerTelText.Width;
            this.picFHouseOwnerTelPic.Height = this.txtFHouseOwnerTelText.Height;
            



        }

        private void HouseSrcInfoWebDCLook_BasicControl_Paint(object sender, PaintEventArgs e)
        {
            if (isIMGshow)
            {
                //txtFTaxPayerNo.BackColor = Color.Transparent;
                //cboFTaxUnitCd.BackColor = Color.Transparent;
                //txtFAddr.BackColor = Color.Transparent;
                e.Graphics.DrawImage(Properties.Resources.ruku, new Point(16, 150));
            }

            //查看次数
            lblFLookCnt.Visible = false;
            e.Graphics.DrawString("已查看", lblFLookCnt.Font, new
  SolidBrush(lblFLookCnt.ForeColor), lblFLookCnt.Left, lblFLookCnt.Top);

            Font newFont = new Font(lblFLookCnt.Font, FontStyle.Bold);//粗体
            SizeF size1 = e.Graphics.MeasureString("已查看", lblFLookCnt.Font);  //计算已经画了字符串的大小 计算已经画了字符串的大小;
            e.Graphics.DrawString(intLookCnt.ToString(), newFont, new
  SolidBrush(System.Drawing.Color.Red), lblFLookCnt.Left+(int)size1.Width, lblFLookCnt.Top);

            SizeF size2 = e.Graphics.MeasureString(intLookCnt.ToString(), newFont);  //计算已经画了字符串的大小 计算已经画了字符串的大小;
            e.Graphics.DrawString("次", lblFLookCnt.Font, new
  SolidBrush(lblFLookCnt.ForeColor), lblFLookCnt.Left + (int)size1.Width + (int)size2.Width, lblFLookCnt.Top);



        }

        /// <summary>
        /// 租用时价格
        /// </summary>
        /// <returns></returns>
        private string RentPrice(decimal? oldPrice)
        {
            if (oldPrice != null)
            {
                string strNewPrice = "";
                strNewPrice = oldPrice.Value.ToString("n2");
                strNewPrice = Regex.Replace(strNewPrice, @"\.00", "");
                return strNewPrice;
            }
            else
            {
                return "";
            }
        }

        /// <summary>
        /// 出售时价格 价格转换为万元
        /// </summary>
        /// <param name="oldPrice"></param>
        /// <returns></returns>
        private string SalePrice(decimal? oldPrice)
        {
            if (oldPrice != null)
            {
                decimal newPrice = 0;
                string strNewPrice = "";
                newPrice = oldPrice.Value / 10000;
                strNewPrice = newPrice.ToString("n2");
                strNewPrice = Regex.Replace(strNewPrice, @"\.00", "");
                return strNewPrice;
            }
            else
            {
                return "";
            }
            
        }


       










    }
}
